/**
  ******************************************************************************
  * @file    
  * @author  
  * @version 
  * @date    
  * @brief   
  ******************************************************************************
	* @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "hwconf.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Initialize the FLASH interface.
  * @param  None
  * @retval None
  */
void FLASH_Config(void)
{
  /* Disable 64-bit access */
  FLASH->ACR &= ~FLASH_ACR_ACC64;
    
  /* Disable Prefetch Buffer */
  FLASH->ACR &= ~FLASH_ACR_PRFTEN;

  /* Flash 0 wait state */
  FLASH->ACR &= ~FLASH_ACR_LATENCY;
}


/**
  * @brief  Initialize the RCC.
  * @param  None
  * @retval None
  */
void RCC_Config(void)
{
	/* HCLK = SYSCLK /1*/
  RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV8;
  
  /* PCLK2 = HCLK /1*/
  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
    
  /* PCLK1 = HCLK /1*/
  RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;	
	
  PWR_SetVoltageRange(SET);
		
  /* Switch to 4 MHz at power on reset */
  RCC_SwitchTo4MHz();

#if (ENABLE_MCO == 1)	
	/* Set MCO for SYSCLK */
  RCC_MCOConfig(RCC_MCOSource_SYSCLK, RCC_MCODiv_1);
#endif
	
}


/**
  * @brief  Switch to internal 16MHz High-Speed oscilator.
  * @param  None
  * @retval None
  */
void RCC_SwitchTo16MHz(void)
{
	/* Enable HSI - High-Speed Internal RC Oscilator */
  *(__IO uint32_t *) RCC_CR_HSION_BB = (uint32_t)SET;
	
	/* Wait till HSI is stable and ready */
	while ((*(__IO uint32_t *)RCC_CR_HSIRDY_BB) == RESET)
	{
	}
	
	/* Set the voltage range to be able to have SYSCLK at 16Mhz */
	PWR_SetVoltageRange(RESET);
	
	/* Set HSI as system clock source */
	*(__IO uint32_t *) RCC_CFGR_SW0_BB = (uint32_t)SET;
		
	/* Wait until the HSI is used as system clock source */	
	while ((*(__IO uint32_t *)RCC_CFGR_SWS0_BB) == RESET)
	{
	}
  
	/* Finally, turn off the MSI */
	*(__IO uint32_t *) RCC_CR_MSION_BB = (uint32_t)RESET;
}


/**
  * @brief  Switch to internal Multi-Speed oscilator and run at 4MHz.
  * @param  None
  * @retval None
  */
void RCC_SwitchTo4MHz(void)
{
	uint32_t temp_reg_value = 0;
	
	PWR_SetVoltageRange(SET);
	
	/* Enable MSI - Multi-Speed Internal RC Oscilator */
  *(__IO uint32_t *) RCC_CR_MSION_BB = (uint32_t)SET;
	
	/* Read the RCC Internal Clock Sources Calibration Register */
	temp_reg_value = RCC->ICSCR;
	
  /* Clear MSIRANGE[2:0] bits */
  temp_reg_value &= ~RCC_ICSCR_MSIRANGE;
  
  /* Set the MSIRANGE[2:0] bits according to RCC_MSIRange_6 ~ 4.194 MHz */
  temp_reg_value |= (uint32_t)RCC_MSIRange_6;
	
	/* Save the configuration */
	RCC->ICSCR = temp_reg_value;
	
	/* Wait till MSI is stable and ready */
	while ((*(__IO uint32_t *)RCC_CR_MSIRDY_BB) == RESET)
	{
	}
	
	/* Set MSI as system clock source */
	*(__IO uint32_t *) RCC_CFGR_SW0_BB = (uint32_t)RESET;
	
	/* Wait until the MSI is used as system clock source */	
	while ((*(__IO uint32_t *) RCC_CFGR_SWS0_BB) == SET)
  {
  }
		
	/* Finally, turn off the HSI */
	*(__IO uint32_t *) RCC_CR_HSION_BB = (uint32_t)RESET;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
